home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 376-400 / disk_388 / free / strings.c < prev   
C/C++ Source or Header  |  1992-05-06  |  3KB  |  110 lines

  1. /***************************************************************************
  2.  * strings.c:    Miscellaneous string functions.
  3.  *
  4.  * Part of...
  5.  *
  6.  *    FREE:    Display free space on your disk volumes.
  7.  *        Author:  Daniel Jay Barrett, barrett@cs.jhu.edu.
  8.  *
  9.  * This program is Freely Distributable.  Make all the copies you want
  10.  * and give them away.  Use this code in any way you like.
  11. ***************************************************************************/
  12.  
  13. #include "free.h"
  14.  
  15. /* Compare two strings, ignoring the difference between upper/lower case.
  16.  * If the strings are the same, return 0; else, return 1. */
  17.  
  18. int StrCaseCmp(char *s1, char *s2)
  19. {
  20.     while (*s1 && *s2)
  21.     {
  22.         if (toupper(*s1) != toupper(*s2))
  23.             return(1);    /* Different. */
  24.         s1++, s2++;
  25.     }
  26.     return(0);            /* Same. */
  27. }
  28.  
  29.  
  30. /* This is a safe method for concatenating string "src" onto the end of
  31.  * string "dest".  If dest does not have enough room to fit "src", an
  32.  * error routine is called, and FALSE is returned.  Otherwise, the
  33.  * concatenation takes place, and TRUE is returned.
  34.  *
  35.  * The flag "ok" is static.  When one error sets "ok" to FALSE, we never
  36.  * need to run this function anymore. */
  37.  
  38. int Concat(char dest[], char src[])
  39. {
  40.     static int ok = TRUE;
  41.     int destLen;
  42.  
  43.     if (ok)
  44.     {
  45.         destLen = strlen(dest);
  46.         if (destLen + strlen(src) <  memSize)    /* memSize global. */
  47.             strcat(dest, src);
  48.         else
  49.         {
  50.             ErrorMsg(ERROR_TOO_SMALL);
  51.             if (destLen > 0)
  52.                 dest[destLen-1] = '\n';
  53.             else
  54.                 dest[0] = '\0';
  55.             ok = FALSE;
  56.         }
  57.     }
  58. }
  59.  
  60.  
  61. /* Create an array of size memSize and return a pointer to it.  We also
  62.  * copy the empty string into the array, in preparation for later
  63.  * strcat() calls. */
  64.     
  65. char *MakeOutArray()
  66. {
  67.     char *arr = NULL;
  68.  
  69.     /* Note that memSize is global, and set by GetOptions. */
  70.  
  71.     if ((memSize <= 0) || !(arr = (char *)malloc(memSize)))
  72.         ExitCleanly((char *)NULL, ERROR_CANT_MALLOC, RETURN_FAIL);
  73.     else
  74.     {
  75.         strcpy(arr, "");
  76.         return(arr);
  77.     }
  78. }
  79.  
  80.  
  81. /* We turn "format[]" into a format string suitable for printf().
  82.  * We create this format string because we want proper indenting of
  83.  * all our output.  The key value to watch is the %d value, which is
  84.  * calculated using the length of the volume name and the default spacing.
  85.  * We set "d_or_s" to be 'd' or 's', depending on whether we want a
  86.  * decimal field or a string field to be last in the format string.
  87.  * We pass "cr" as TRUE if we want a carriage return (really a newline)
  88.  * at the end of the format string. */
  89.  
  90. void MakeFormatString(char *volume, char format[], char d_or_s, int cr)
  91. {
  92.     char metaFormat[FORMAT_LENGTH];
  93.  
  94.     if (d_or_s == 'd')
  95.         strcpy(metaFormat, "%%s%%s%%s%%%dld%s");
  96.     else if (d_or_s == 's')
  97.         strcpy(metaFormat, "%%s%%s%%s%%%ds%s");
  98.     else
  99.         ErrorMsg(ERROR_IMPOSSIBLE);
  100.  
  101.     /* volumeNameLen is global and set in InitializeGlobals() or
  102.      * GetOptions(). */
  103.  
  104.     sprintf(format, metaFormat, 
  105.         DEFAULT_SPACING + volumeNameLen - strlen(volume),
  106.         cr ? "\n" : "");
  107. }
  108.  
  109.  
  110.